package com.yunwaikeji.gy_tool_library.set;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.info.GYBaseInfo;
import com.yunwaikeji.gy_tool_library.info.GYDisplayInfo;
import com.yunwaikeji.gy_tool_library.info.GYViewInfo;
import com.yunwaikeji.gy_tool_library.interface1.GYViewBgBuilderInterface;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;

public class GYViewBgBuilder<V, VS> implements GYViewBgBuilderInterface<V, VS>, GYBaseInfo {
    public View view;
    public V v;
    public VS vs;

    private Drawable backgroundDrawable;//背景图
    private Drawable changeDrawable;//改变的图
    private int changeEffect;//改变效果，如按压
    private boolean isChangeBackground;//是否允许改变背景
    private int displayStyle;//显示效果，如椭圆
    private int edgeWidthPx;//边线宽度
    private int edgeColor;//边线颜色
    private boolean edgeOver;//边线是否覆盖背景图
    private int radiusLeftTopPx;//左上圆角
    private int radiusRightTopPx;//右上圆角
    private int radiusRightBottomPx;//右下圆角
    private int radiusLeftBottomPx;//做下圆角

    public GYViewBgBuilder(View view, AttributeSet attributeSet) {
        this.view = view;
        clearAllGround();
        v = (V) view;
        vs = (VS) this;
        int radiusPx = WRAP_CONTENT;
        if (attributeSet != null) {
            TypedArray typedArray = view.getContext().obtainStyledAttributes(attributeSet, R.styleable.GYView);
            backgroundDrawable = typedArray.getDrawable(R.styleable.GYView_android_background);
            changeDrawable = typedArray.getDrawable(R.styleable.GYView_changeground);
            changeEffect = typedArray.getInteger(R.styleable.GYView_changeEffect, changeEffect);
            isChangeBackground = typedArray.getBoolean(R.styleable.GYView_isChangeBackground, isChangeBackground);
            displayStyle = typedArray.getInteger(R.styleable.GYView_displayStyle, displayStyle);
            radiusPx = typedArray.getDimensionPixelSize(R.styleable.GYView_radius, radiusPx);
            radiusLeftTopPx = typedArray.getDimensionPixelSize(R.styleable.GYView_radiusLeftTop, radiusLeftTopPx);
            radiusRightTopPx = typedArray.getDimensionPixelSize(R.styleable.GYView_radiusRightTop, radiusRightTopPx);
            radiusRightBottomPx = typedArray.getDimensionPixelSize(R.styleable.GYView_radiusRightBottom, radiusRightBottomPx);
            radiusLeftBottomPx = typedArray.getDimensionPixelSize(R.styleable.GYView_radiusLeftBottom, radiusLeftBottomPx);
            edgeWidthPx = typedArray.getDimensionPixelSize(R.styleable.GYView_edgeWidth, edgeWidthPx);
            edgeColor = typedArray.getColor(R.styleable.GYView_edgeColor, edgeColor);
            edgeOver = typedArray.getBoolean(R.styleable.GYView_edgeOver, edgeOver);
            typedArray.recycle();
        }
        if (radiusPx != WRAP_CONTENT) {
            radiusLeftTopPx = radiusPx;
            radiusRightTopPx = radiusPx;
            radiusRightBottomPx = radiusPx;
            radiusLeftBottomPx = radiusPx;
        }
        build();
    }

    @Override
    public V clearAllGround() {
        backgroundDrawable = null;//背景图
        changeDrawable = null;//改变的图
        changeEffect = GYViewInfo.ChangeEffect.PRESS;//改变效果，如按压
        isChangeBackground = true;//是否允许改变背景
        displayStyle = GYDisplayInfo.DisplayStyle.NONE;//显示效果，如椭圆
        edgeWidthPx = 0;//边线宽度
        edgeColor = Color.LTGRAY;//边线颜色
        edgeOver = false;//边线是否覆盖背景图
        radiusLeftTopPx = 0;//左上圆角
        radiusRightTopPx = 0;//右上圆角
        radiusRightBottomPx = 0;//右下圆角
        radiusLeftBottomPx = 0;//做下圆角
        view.setBackgroundColor(Color.TRANSPARENT);
        return v;
    }

    public GYViewBgBuilder(View view) {
        this.view = view;
    }

    @Override
    public VS setBackground(Drawable background) {
        backgroundDrawable = background;
        return vs;
    }

    @Override
    public Drawable getBackground() {
        return backgroundDrawable;
    }

    @Override
    public VS setBackgroundColor(int backgroundColor) {
        return setBackground(new ColorDrawable(backgroundColor));
    }

    @Override
    public VS setBackgroundColors(int... backgroundColor) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColors(backgroundColor);
        return setBackground(gradientDrawable);
    }

    @Override
    public VS setChangeEffect(int changeEffect) {
        this.changeEffect = changeEffect;
        return vs;
    }

    @Override
    public int getChangeEffect() {
        return changeEffect;
    }

    @Override
    public VS setChangeground(Drawable changeDrawable) {
        this.changeDrawable = changeDrawable;
        return vs;
    }

    @Override
    public Drawable getChangeground() {
        return changeDrawable;
    }

    @Override
    public VS setChangeColor(int changeColor) {
        return setChangeground(new ColorDrawable(changeColor));
    }

    @Override
    public VS setChangeColors(int... changeColors) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColors(changeColors);
        return setChangeground(gradientDrawable);
    }

    @Override
    public VS setDisplayStyle(int displayStyle) {
        this.displayStyle = displayStyle;
        return vs;
    }

    @Override
    public int getDisplayStyle() {
        return displayStyle;
    }

    @Override
    public VS setRadiusPx(int radiusPx) {
        setRadiusLeftTopPx(radiusPx);
        setRadiusRightTopPx(radiusPx);
        setRadiusRightBottomPx(radiusPx);
        setRadiusLeftBottomPx(radiusPx);
        return vs;
    }

    @Override
    public VS setRadiusDp(int radiusDp) {
        return setRadiusPx(GYDisplayUtils.dp2px(view.getContext(), radiusDp));
    }

    @Override
    public VS setRadiusLeftTopPx(int radiusLeftTopPx) {
        this.radiusLeftTopPx = radiusLeftTopPx;
        return vs;
    }

    @Override
    public int getRadiusLeftTopPx() {
        return radiusLeftTopPx;
    }

    @Override
    public VS setRadiusLeftTopDp(int radiusLeftTopDp) {
        return setRadiusLeftTopPx(GYDisplayUtils.dp2px(view.getContext(), radiusLeftTopDp));
    }

    @Override
    public VS setRadiusRightTopPx(int radiusRightTopPx) {
        this.radiusRightTopPx = radiusRightTopPx;
        return vs;
    }

    @Override
    public int getRadiusRightTopPx() {
        return radiusRightTopPx;
    }

    @Override
    public VS setRadiusRightTopDp(int radiusRightTopDp) {
        return setRadiusRightTopPx(GYDisplayUtils.dp2px(view.getContext(), radiusRightTopDp));
    }

    @Override
    public VS setRadiusRightBottomPx(int radiusRightBottomPx) {
        this.radiusRightBottomPx = radiusRightBottomPx;
        return vs;
    }

    @Override
    public int getRadiusRightBottomPx() {
        return radiusRightBottomPx;
    }

    @Override
    public VS setRadiusRightBottomDp(int radiusRightBottomDp) {
        return setRadiusRightBottomPx(GYDisplayUtils.dp2px(view.getContext(), radiusRightBottomDp));
    }

    @Override
    public VS setRadiusLeftBottomPx(int radiusLeftBottomPx) {
        this.radiusLeftBottomPx = radiusLeftBottomPx;
        return vs;
    }

    @Override
    public int getRadiusLeftBottomPx() {
        return radiusLeftBottomPx;
    }

    @Override
    public VS setRadiusLeftBottomDp(int radiusLeftBottomDp) {
        return setRadiusLeftBottomPx(GYDisplayUtils.dp2px(view.getContext(), radiusLeftBottomDp));
    }

    @Override
    public VS setEdgeWidthPx(int edgeWidthPx) {
        this.edgeWidthPx = edgeWidthPx;
        return vs;
    }

    @Override
    public int getEdgeWidthPx() {
        return edgeWidthPx;
    }

    @Override
    public VS setEdgeWidthDp(int edgeWidthDp) {
        return setEdgeWidthPx(GYDisplayUtils.dp2px(view.getContext(), edgeWidthDp));
    }

    @Override
    public VS setEdgeColor(int edgeColor) {
        this.edgeColor = edgeColor;
        return vs;
    }

    @Override
    public int getEdgeColor() {
        return edgeColor;
    }

    @Override
    public VS setEdgeOver(boolean edgeOver) {
        this.edgeOver = edgeOver;
        return vs;
    }

    @Override
    public boolean isEdgeOver() {
        return edgeOver;
    }

    @Override
    public VS setIsChangeBackground(boolean changeBg) {
        this.isChangeBackground = changeBg;
        return vs;
    }

    @Override
    public boolean isChangeBackground() {
        return isChangeBackground;
    }

    @Override
    public V build() {
        view.setBackground(GYDisplayUtils.newBuilder()
                .setNormal(backgroundDrawable)
                .setChange(changeDrawable)
                .setRadiusLeftTopPx(radiusLeftTopPx)
                .setRadiusRightTopPx(radiusRightTopPx)
                .setRadiusRightBottomPx(radiusRightBottomPx)
                .setRadiusLeftBottomPx(radiusLeftBottomPx)
                .setEdgeWidthPx(edgeWidthPx)
                .setEdgeColor(edgeColor)
                .setEdgeOver(edgeOver)
                .setChangeEffect(isChangeBackground ? changeEffect : GYDisplayInfo.ChangeEffect.NONE)
                .setDisplayStyle(displayStyle)
                .buildDrawable());
        return v;
    }
}
